/*
 * @Description: 添加、编辑登记通用表单
 * @Author: Rfan
 * @Date: 2022-11-19 16:31:39
 * @LastEditTime: 2022-12-08 16:30:16
 */

import { useEffect, useState } from 'react';
import { Form, Input, InputNumber, Select } from 'antd';
import JPDatePicker from '@/components/JPComponents/JPDatePicker';
import { appState } from '@/store/mobx';
import Fond from '@/domains/fond/entities/fond';
import ArchiveType from '@/domains/archiveType/entities/archiveType';
import fondService from '@/domains/fond/fondService';
import archiveTypeService from '@/domains/archiveType/archiveTypeService';
import type { IFond } from '@/interfaces/fond';
import type { IArchiveType } from '@/interfaces/archiveType';

interface IDestructionFormProps {
  visible: boolean;
}

const DestructionForm = (props: IDestructionFormProps) => {
  const { visible } = props;
  const form = Form.useFormInstance();
  const selectFond = Form.useWatch('fondsId', form);

  // 全宗列表
  const [fondList, setFondList] = useState<IFond[]>([]);
  // 档案类型列表
  const [ATList, setATList] = useState<IArchiveType[]>([]);

  useEffect(() => {
    if (visible) {
      fondService.getFondList(appState.unitId).then((list) => {
        setFondList(list.map((item: Fond) => ({ value: item.id, label: item.fondName })));
      });
    }
  }, [visible]);

  useEffect(() => {
    if (selectFond) {
      archiveTypeService.getATList(selectFond).then((list) => {
        setATList(list.map((item: ArchiveType) => ({ value: item.id, label: item.atName })));
      });
    }
  }, [selectFond]);

  return (
    <>
      <Form.Item name="registrationTime" label="登记时间">
        <JPDatePicker showTime />
      </Form.Item>
      <Form.Item label="销毁授权">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="fondsId" label="全宗" rules={[{ required: true }]}>
        <Select options={fondList} />
      </Form.Item>
      <Form.Item name="archiveTypeId" label="档案类型" rules={[{ required: true }]}>
        <Select options={ATList} />
      </Form.Item>
      <Form.Item name="year" label="所属年度">
        <Input allowClear />
      </Form.Item>
      <Form.Item label="档案移交单位">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="unitName" label="档案形成单位">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="category" label="分类">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="retentionPeriod" label="保管期限">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="secret" label="密级">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="folderCount" label="卷数">
        <InputNumber min={0} />
      </Form.Item>
      <Form.Item name="fileCount" label="件数">
        <InputNumber min={0} />
      </Form.Item>
      <Form.Item name="efileCount" label="电子文件数">
        <InputNumber min={0} />
      </Form.Item>
      <Form.Item name="pageCount" label="页">
        <InputNumber min={0} />
      </Form.Item>
      <Form.Item label="字节数">
        <InputNumber min={0} />
      </Form.Item>
      <Form.Item name="duration" label="分钟">
        <InputNumber min={0} />
      </Form.Item>
      <Form.Item name="archiveBoxName" label="盒">
        <Input allowClear />
      </Form.Item>
      <Form.Item name="boxTotalWidth" label="米">
        <InputNumber min={0} />
      </Form.Item>
    </>
  );
};

export default DestructionForm;
